<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input clearable v-model="queryForm.name"></el-input>
      </el-col>
      <el-col :span="7">
        <el-button type="primary" :icon="Search" @click="initGetUserList">
          搜索</el-button
        >
        <el-button type="primary" @click="addUserFunction()"
          >添加用户</el-button
        ></el-col
      >
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column
        v-for="(item, index) in columns"
        :prop="item.prop"
        :label="item.label"
        :width="item.with"
        :key="index"
      >
        <template v-slot="{ row }" v-if="item.prop === 'isDel'">
          <el-switch
            plain
            :inactive-value="0"
            :active-value="1"
            v-model="row.isDel"
            @change="changeValue(row)"
          />
        </template>
        <template v-slot="{ row }" v-if="item.prop === 'timeMillions'">
          {{ $filters.filterTime(row.timeMillions) }}
        </template>
        <template #default="{ row }" v-if="item.prop === 'action'">
          <el-button
            link
            type="primary"
            :icon="Edit"
            @click="addUserFunction(row)"
          >
          </el-button>
          <el-button link type="primary" :icon="Setting"></el-button>
          <el-button link type="primary" :icon="Delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      v-model:current-page="queryForm.pageNo"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 2, 50, 100]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="queryForm.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <AddUser
    v-model="addUserDialog"
    :dialogTitle="dialogTitle"
    :dialogRowValue="dialogRowValue"
    :enterStr="enterStr"
    @initGetUserList="initGetUserList"
    v-if="addUserDialog"
  ></AddUser>
</template>
<script setup>
import { Search, Delete, Edit, Setting } from "@element-plus/icons-vue";
import { ref, toRaw } from "vue";
import { getUserList, changeUserState } from "@/api/user.js";
import { columns } from "./UserColumn";
import AddUser from "./components/addUser.vue";
import { isNull } from "@/utils/filters.js";

const addUserDialog = ref(false);
const dialogTitle = ref("");
const dialogRowValue = ref({});
const enterStr = ref("");
// 弹出新增的窗  将弹窗的属性值设置为true就可以展示了
const addUserFunction = (row) => {
  if (isNull(row)) {
    dialogTitle.value = "添加用户";
    dialogRowValue.value = {};
    enterStr.value = "add";
  } else {
    dialogTitle.value = "修改用户";
    dialogRowValue.value = JSON.parse(JSON.stringify(row));
    enterStr.value = "edit";
  }
  addUserDialog.value = true;
};

// 搜索时，提交的表单内容
const queryForm = ref({
  name: "",
  pageNo: 1,
  pageSize: 2,
  total: 0,
});

// 删除用户时的弹窗
const open = (row) => {
  ElMessageBox.confirm("确定停用?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then((action) => {
      if ("confirm" === action) {
        changeState(row);
        ElMessage({
          type: "success",
          message: "删除成功",
        });
      }
      if ("cancel" === action) {
        if (row.isDel == 0) {
          row.isDel = 1;
        } else {
          row.isDel = 0;
        }
      }
    })
    .catch(() => {
      if (row.isDel == 0) {
        row.isDel = 1;
      } else {
        row.isDel = 0;
      }
      ElMessage({
        type: "info",
        message: "取消",
      });
    });
};

const changeValue = (row) => {
  open(row);
};

const tableData = ref([]);

// 改变每页条数
const handleSizeChange = (val) => {
  queryForm.value.pageNo = 1;
  queryForm.value.pageSize = val;
  initGetUserList();
};
// 改变页码
const handleCurrentChange = (val) => {
  queryForm.value.pageNo = val;
  initGetUserList();
};

// 获取用户数据
const initGetUserList = async () => {
  console.log("查询列表");
  if (queryForm.value.name === "") {
    queryForm.value.name = null;
  }
  const res = await getUserList(queryForm.value);
  console.log(res);
  tableData.value = res.data.data;
  queryForm.value.total = res.data.total;
  return res;
};

initGetUserList();

const changeState = async (data) => {
  const res = await changeUserState(data);
};
</script>
<style scoped>
.header {
  padding: 16px;
  box-sizing: border-box;
}

::v-deep .el-pagination {
  padding: 16px;
  display: flex;
  justify-content: flex-end;
}

::v-deep .el-row header {
  text-align: right;
}
</style>
